home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / superfly source / linkedlist.cp < prev    next >
Encoding:
Text File  |  1996-01-17  |  1.9 KB  |  120 lines

  1. /*
  2.     File:        LinkedList.cp
  3.  
  4.     Contains:    A lame linked list class
  5.                 
  6.     Written by: Kent Miller
  7.     
  8.     Copyright:    © 1995 Apple Computer
  9.  
  10.     Notes:        When I started writing this sample code, there wasn't a
  11.                 LinkedList class in Sprocket.  In Dave's January 96 update
  12.                 to Sprocket, he included a better linked list class but I'm
  13.                 too lazy to revise this program use his class.
  14.  
  15.     Change History (most recent first):
  16.  
  17.  */
  18.  
  19. #include "LinkedList.h"
  20.  
  21. TLinkedList::TLinkedList()
  22.     {
  23.     this->head = nil;
  24.     }
  25.     
  26. TLinkedList::~TLinkedList()
  27.     {
  28.     if (this->head != nil)
  29.         {
  30.         while (this->head != nil)
  31.             this->RemoveFromList(this->head->elem);
  32.         
  33.         }
  34.     }
  35.     
  36. void *
  37. TLinkedList::RemoveFromList (void * obj)
  38.     {
  39.     ListElem *trailer = this->head;
  40.     ListElem *theList = this->head;
  41.     void * elem;
  42.     
  43.     while ((theList != nil) && (theList->elem != obj))
  44.         {
  45.         trailer = theList;
  46.         theList = theList->next;
  47.         }
  48.     
  49.     if (theList == nil)
  50.         return nil;
  51.  
  52.     elem = theList->elem;
  53.     if (theList == this->head)
  54.         {
  55.         this->head = this->head->next;
  56.         DisposePtr ( (Ptr) theList);
  57.         }
  58.     else
  59.         {        
  60.         trailer->next = theList->next;
  61.         DisposePtr ( (Ptr) theList);
  62.         }
  63.     
  64.     return elem;
  65.     }
  66.     
  67.     
  68. void
  69. TLinkedList::AddToList (void * obj)
  70.     {
  71.     ListElem *theList;
  72.     
  73.     theList = (ListElem *) NewPtr (sizeof (ListElem));
  74.     theList->elem = obj;
  75.     theList->next = this->head;
  76.     this->head = theList;
  77.     }
  78.     
  79. void *    
  80. TLinkedList::GetFirstListElem()
  81.     {
  82.     if (this->head)
  83.         return this->head->elem;
  84.     else
  85.         return nil;
  86.     }
  87.  
  88. SInt16
  89. TLinkedList::CountListItems()
  90.     {
  91.     ListElem     * theList = this->head;
  92.     SInt16         count = 0;
  93.  
  94.     while (theList)
  95.         {
  96.         count++;
  97.         theList = theList->next;
  98.         }
  99.     
  100.     return count;
  101.     }
  102.  
  103. void *    
  104. TLinkedList::GetNextListElem(void * obj)
  105.     {
  106.     ListElem     * theList = this->head;
  107.  
  108.     while ((theList != nil) && (theList->elem != obj))
  109.         theList = theList->next;
  110.         
  111.     if (theList == nil)
  112.         return nil;
  113.     else if (theList->next == nil)
  114.         return nil;
  115.     else
  116.         return theList->next->elem;
  117.  
  118.     }
  119.  
  120.